package com.lincoln.redis.server.config;

import com.lincoln.redis.server.utils.SpringBeanFactoryUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 重写ClientDetailsService,功能与UserDetailsService相同
 *
 * @author admin
 */
public class MyClientDetailsService implements ClientDetailsService {
    public static final List<String> resourceIds = new ArrayList<String>() {{
        add("redis-server");
    }};
    public static final List<String> authorizedGrantTypes = new ArrayList<String>() {{
        add("password");
        add("refresh_token");
    }};
    public static final Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>() {{
        add(new SimpleGrantedAuthority("ordinary"));
    }};
    public static final Set<String> scope = new HashSet<String>() {{
        add("redis-server");
    }};

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        BaseClientDetails clientDetails = new BaseClientDetails();
        clientDetails.setClientId(clientId);
        clientDetails.setResourceIds(resourceIds);
        clientDetails.setScope(scope);
        clientDetails.setClientSecret(PasswordEncoderFactories.createDelegatingPasswordEncoder().encode("123456"));
        clientDetails.setAuthorizedGrantTypes(authorizedGrantTypes);
        clientDetails.setAuthorities(authorities);
        clientDetails.setAccessTokenValiditySeconds(900);
        clientDetails.setRefreshTokenValiditySeconds(3600);
        return clientDetails;
    }
}
